package pl.planuz;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class PlanActivity extends Activity implements OnClickListener{
	TextView selection;
	public int idToModify;
	database dm;
	Helper help;
	String ngrupa;
	boolean uruchom=false;
	GridView gridView;
	View b[]= new View[8];
	View kreator;
	private static PlanActivity instance;
	TableLayout table;
    TableRow row1;
    TableRow row2;
    ListView listView1;
    ListView listView2;
    ListView listView3;
    ListView listView4;
    ListView listView5;
    ListView listView6;

	List<String[]> list = new ArrayList<String[]>();
	List<String[]> names2 =null ;
	List<String[]> names3 =null,names4=null,names5=null ;
	String[] stg1;
	String[] stg144,stg155;
	int year, month, day;
	public PlanActivity() {
	        instance = this;
	    }

	    public static Context getContext() {
	        return instance;
	    }

	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.plan);
		b[0]=findViewById(R.id.Poniedzialek);
		b[1]=findViewById(R.id.Wtorek);
		b[2]=findViewById(R.id.Sroda);
		b[3]=findViewById(R.id.Czwartek);
		b[4]=findViewById(R.id.Piatek);
		b[5]=findViewById(R.id.Sobota);
		b[6]=findViewById(R.id.Niedziela);
		b[7]=findViewById(R.id.Niestandardowe);
		kreator=findViewById(R.id.button1);
		kreator.setOnClickListener(this);
		for(int i=0;i<8;i++){
			b[i].setOnClickListener(this);
		}
		table = new TableLayout(this);  
		  
	    table.setStretchAllColumns(true);  
	    table.setShrinkAllColumns(true);   
	  
	    row1 = new TableRow(this);  
	    row2 = new TableRow(this);  
	    listView1 = (ListView) findViewById(R.id.listView1);
	    listView2 = (ListView) findViewById(R.id.listView2);
	    listView3 = (ListView) findViewById(R.id.listView3);
	    listView4 = (ListView) findViewById(R.id.listView4);
	    listView5 = (ListView) findViewById(R.id.listView5);
	    listView6 = (ListView) findViewById(R.id.listView6);
		if(uruchom==false){
			help=new Helper();
			uruchom=true;
			}
		dm = new database(this,Helper.getwydzial(),Helper.getkierunek(),null);
		GregorianCalendar date = new GregorianCalendar( year, month, day ); 
			switch(date.get(Calendar.DAY_OF_WEEK)){
			case Calendar.MONDAY:
				names2=dm.selectP(1);
				break;
			case Calendar.TUESDAY:
				names2=dm.selectP(2);
				break;
			case Calendar.WEDNESDAY:
				names2=dm.selectP(3);
				break;
			case Calendar.THURSDAY:
				names2=dm.selectP(4);
				break;
			case Calendar.FRIDAY:
				names2=dm.selectP(5);
				break;
			case Calendar.SATURDAY:
				names2=dm.selectP(6);
				break;
			case Calendar.SUNDAY:
				names2=dm.selectP(7);
				break;
			}
				
				stg1=new String[names2.size()]; 
				int x=0;
				String stg;
				boolean brak=false;
				for (String[] wydzial : names2) 
				{
					stg = wydzial[4]+"-"+wydzial[5];
					stg1[x]=stg;
					x++;
				}
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(   
						this,R.layout.editlist,   
						stg1);
				listView1.setAdapter(adapter);
				stg1=new String[names2.size()]; 
				int x2=0;
				String stg2;
				for (String[] wydzial : names2) 
				{
					stg2 = wydzial[6];
					stg1[x2]=stg2;
					x2++;
				}
				ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(   
						this,R.layout.editlist,   
						stg1);
				listView2.setAdapter(adapter2);
				stg1=new String[names2.size()]; 
				int x3=0;
				String stg3;
				for (String[] wydzial : names2) 
				{
					stg3 = wydzial[7];
					stg1[x3]=stg3;
					x3++;
				}
				if(x3==1)brak=true;
				ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(   
						this,R.layout.editlist,   
						stg1);
				listView3.setAdapter(adapter3);
				stg1=new String[names2.size()]; 
				int x4=0;
				String stg4;
				for (String[] wydzial : names2) 
				{
					stg4 = wydzial[0];
					stg1[x4]=stg4;
					System.out.println("nr: "+stg1[x4]);
					x4++;
				}
				String stg44 = "";
				String tmp = null;
				stg144=new String[stg1.length];
				
				for(int i=0;i<stg1.length;i++){
					if(brak==true){stg144[i]="";}else{
					names3=dm.selectWyk(stg1[i]);
					stg44="";
					for (String[] wydzial : names3){
					tmp=wydzial[2];
					stg44 =tmp+","+stg44;
					}
					stg144[i]=stg44;
					}
					System.out.println("wyk: "+i+" "+stg144[i]);
				}
				ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(   
						this,R.layout.editlist,   
						stg144);
				listView4.setAdapter(adapter4);
				String stg55 = "";
				String tmp5 = null;
				String tmp6 = null;
				stg155=new String[stg1.length];
				for(int i=0;i<stg1.length;i++){
					names4=dm.selectSala(stg1[i]);
					names5=dm.selectBud(stg1[i]);
					stg55="";
					for (String[] bud : names5){
						tmp6=bud[2];
						stg55=tmp6;
					for (String[] wydzial : names4){
					tmp5=wydzial[2];
					stg55 =tmp5+","+stg55;
					}
					}
					stg155[i]=stg55;
					System.out.println("sala: "+i+" "+stg155[i]);
				}
				ArrayAdapter<String> adapter5 = new ArrayAdapter<String>(   
						this,R.layout.editlist,   
						stg155);
				listView5.setAdapter(adapter5);
				
				stg1=new String[names2.size()]; 
				int x6=0;
				String stg6;
				for (String[] wydzial : names2) 
				{
					stg6 = wydzial[8];
					stg1[x6]=stg6;
					x6++;
				}
				ArrayAdapter<String> adapter6 = new ArrayAdapter<String>(   
						this,R.layout.editlist,   
						stg1);
				listView6.setAdapter(adapter6);
		}
		
	public void onClick(View arg0) {
		switch(arg0.getId()){
		case R.id.Poniedzialek:
			names2=dm.selectP(1);
			break;
		case R.id.Wtorek:
			names2=dm.selectP(2);
			break;
		case R.id.Sroda:
			names2=dm.selectP(3);
			break;
		case R.id.Czwartek:
			names2=dm.selectP(4);
			break;
		case R.id.Piatek:
			names2=dm.selectP(5);
			break;
		case R.id.Sobota:
			names2=dm.selectP(6);
			break;
		case R.id.Niedziela:
			names2=dm.selectP(7);
			break;
		case R.id.Niestandardowe:
			names2=dm.selectP(8);
			break;
		case R.id.button1:
			Intent i2 = new Intent(this, PlanUZActivity.class);
			PlanUZActivity.wykonaj(true);
			startActivity(i2);
			break;
		}
			
			stg1=new String[names2.size()]; 
			int x=0;
			String stg;
			boolean brak=false;
			for (String[] wydzial : names2) 
			{
				stg = wydzial[4]+"-"+wydzial[5];
				stg1[x]=stg;
				x++;
			}
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(   
					this,R.layout.editlist,   
					stg1);
			listView1.setAdapter(adapter);
			stg1=new String[names2.size()]; 
			int x2=0;
			String stg2;
			for (String[] wydzial : names2) 
			{
				stg2 = wydzial[6];
				stg1[x2]=stg2;
				x2++;
			}
			ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(   
					this,R.layout.editlist,   
					stg1);
			listView2.setAdapter(adapter2);
			stg1=new String[names2.size()]; 
			int x3=0;
			String stg3;
			for (String[] wydzial : names2) 
			{
				stg3 = wydzial[7];
				stg1[x3]=stg3;
				x3++;
			}
			if(x3==1)brak=true;
			ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(   
					this,R.layout.editlist,   
					stg1);
			listView3.setAdapter(adapter3);
			stg1=new String[names2.size()]; 
			int x4=0;
			String stg4;
			for (String[] wydzial : names2) 
			{
				stg4 = wydzial[0];
				stg1[x4]=stg4;
				System.out.println("nr: "+stg1[x4]);
				x4++;
			}
			String stg44 = "";
			String tmp = null;
			stg144=new String[stg1.length];
			
			for(int i=0;i<stg1.length;i++){
				if(brak==true){stg144[i]="";}else{
				names3=dm.selectWyk(stg1[i]);
				stg44="";
				for (String[] wydzial : names3){
				tmp=wydzial[2];
				stg44 =tmp+","+stg44;
				}
				stg144[i]=stg44;
				}
				System.out.println("wyk: "+i+" "+stg144[i]);
			}
			ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(   
					this,R.layout.editlist,   
					stg144);
			listView4.setAdapter(adapter4);
			String stg55 = "";
			String tmp5 = null;
			String tmp6 = null;
			stg155=new String[stg1.length];
			for(int i=0;i<stg1.length;i++){
				names4=dm.selectSala(stg1[i]);
				names5=dm.selectBud(stg1[i]);
				stg55="";
				for (String[] bud : names5){
					tmp6=bud[2];
					stg55=tmp6;
				for (String[] wydzial : names4){
				tmp5=wydzial[2];
				stg55 =tmp5+","+stg55;
				}
				}
				stg155[i]=stg55;
				System.out.println("sala: "+i+" "+stg155[i]);
			}
			ArrayAdapter<String> adapter5 = new ArrayAdapter<String>(   
					this,R.layout.editlist,   
					stg155);
			listView5.setAdapter(adapter5);
			
			stg1=new String[names2.size()]; 
			int x6=0;
			String stg6;
			for (String[] wydzial : names2) 
			{
				stg6 = wydzial[8];
				stg1[x6]=stg6;
				x6++;
			}
			ArrayAdapter<String> adapter6 = new ArrayAdapter<String>(   
					this,R.layout.editlist,   
					stg1);
			listView6.setAdapter(adapter6);
	}
}
	

	



